package com.xing.fragment;

import java.util.List;
import com.xing.controller.DBController;
import com.xing.designmode.QuestionAnswer;
import com.xing.designmode.QuestionAnswerFatory;
import com.xing.model.HsdDataResult;
import com.xing.model.HsdModelAnswer;
import com.xing.model.HsdModelQuestion;
import com.xing.questionnairesurvey.R;
import com.xing.util.SurveyUtil;
import com.xing.util.ViewUtil;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SurveyDetailFragment extends Fragment{
	
	private View view;
	private Button backButton,rightButton;
	private LinearLayout leftLayout;
	private LinearLayout rightLayout;
	private FragmentManager fragmentManager;
	private TextView descriptionTextView;
	private TextView currentQuestionNumTV,totalQuestionNumTV;
	
	private List<HsdModelAnswer> currentAnswerModelObjectList;
	
	private LinearLayout answerParentLayout;
	private RadioGroup radioGroup;
	
	private String surveyuuid;
	/**
	 * 问题题号
	 */
	private int currentSortIndex;
	private int questionCount;
	
	private String researchuuid = null;
	/**
	 * 当前问题的uuid
	 */
	private String currentQuestionuuid = null;
	
	/**
	 * 当前问题之前选中的答案
	 */
    private List<HsdDataResult> currrentSelectedHsdDataResultList;
    /**
     * 问题类型
     */
    private String questionType;
	
    private EditText singleEditText;
    
    private String surveyName;
    
    private String opFlag;
    
    private TextView titleTextView; 
    /**
     * 题号
     */
    private TextView currentIndexTextView;
    
    private QuestionAnswer questionAnswer;
    
    private HsdModelQuestion hsdModelQuestion;
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		fragmentManager = getFragmentManager();
		surveyName = null;
		return (view = inflater.inflate(R.layout.survey_detail_fragment,container,false));
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		surveyName = this.getArguments().getString("survey_name");
		surveyuuid = this.getArguments().getString("surveyuuid");
		researchuuid = this.getArguments().getString("researchuuid");
		opFlag = this.getArguments().getString("op_flag");
		
		currentSortIndex = 1;
		
		titleTextView = (TextView) view.findViewById(R.id.title);
		titleTextView.setText(surveyName);
		currentQuestionNumTV = (TextView) view.findViewById(R.id.current_question_num);
		totalQuestionNumTV = (TextView) view.findViewById(R.id.total_question_num);
		backButton = (Button) view.findViewById(R.id.back);
		rightButton = (Button) view.findViewById(R.id.right);
		currentIndexTextView = (TextView) view.findViewById(R.id.currnt_index);
		
		leftLayout = (LinearLayout) view.findViewById(R.id.arrow_left_layout);
		rightLayout = (LinearLayout) view.findViewById(R.id.arrow_right_layout);
		descriptionTextView = (TextView) view.findViewById(R.id.description);
		answerParentLayout = (LinearLayout) view.findViewById(R.id.answer_parent_layout);
		
		
		backButton.setOnClickListener(onMyClickListener);
		leftLayout.setOnClickListener(onMyClickListener);
		rightLayout.setOnClickListener(onMyClickListener);
		rightButton.setOnClickListener(onMyClickListener);
		
		DBController.getInstance().getHsdModelQuestionObject(surveyuuid, String.valueOf(currentSortIndex), getActivity(), hsdModelQuestionHandler);
		DBController.getInstance().getHsdModeSurveyQuestoinNum(getActivity(), surveyuuid,getHsdModelSurveyQuestionNumHandler);
	}
	
	private Handler getHsdModelSurveyQuestionNumHandler = new Handler(){
		public void handleMessage(Message msg) {
			questionCount = (Integer) msg.obj;
			totalQuestionNumTV.setText(String.valueOf(questionCount));
		}
	};
	
	private Handler hsdModelQuestionHandler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			try {
				hsdModelQuestion =  (HsdModelQuestion) msg.obj;
				currentSortIndex = hsdModelQuestion.getSortindex();
				currentQuestionNumTV.setText(String.valueOf(currentSortIndex));
				currentQuestionuuid = hsdModelQuestion.getQuestionuuid();
				descriptionTextView.setText(hsdModelQuestion.getDescription());
				if (currentSortIndex == questionCount) {
					rightButton.setVisibility(View.VISIBLE);
				}else {
					rightButton.setVisibility(View.GONE);
				}
				int type = hsdModelQuestion.getType();
				int typeid = hsdModelQuestion.getTypeid();
				questionType = SurveyUtil.getQuestionType(String.valueOf(typeid), String.valueOf(type));
				DBController.getInstance().
				getHsdModelAnswerList(currentQuestionuuid, surveyuuid, getActivity(), hsdModelAnswerHandler);
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("hsdModelQuestionHandler exception------"+e.getMessage());
			}
			
			
		}
	};
	
	private Handler hsdModelAnswerHandler = new Handler(){
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			currentAnswerModelObjectList = (List<HsdModelAnswer>) msg.obj;
			currentIndexTextView.setText(String.valueOf(currentSortIndex));
			DBController.getInstance().getHsdDataResultList(researchuuid, surveyuuid, currentQuestionuuid, getActivity(), answerResultHandler);
		}
	};
	
	private Handler answerResultHandler = new Handler(){
		@SuppressLint("NewApi")
		public void handleMessage(Message msg) {
			currrentSelectedHsdDataResultList =  (List<HsdDataResult>) msg.obj;
			System.out.println("questionType==="+questionType);
			
			answerParentLayout.removeAllViews();
			
			questionAnswer = QuestionAnswerFatory.createQuestionAnswer(questionType, getActivity(), view, answerParentLayout,radioGroup, currentQuestionuuid,researchuuid, surveyuuid, dealAnswerResultHandler, currrentSelectedHsdDataResultList, currentAnswerModelObjectList,opFlag);
			questionAnswer.showAnswer();
			questionAnswer.showAnswer(hsdModelQuestion);
			
			
		}
	};
	
	private Handler dealAnswerResultHandler = new Handler(){
		public void handleMessage(Message msg) {
			boolean isInserSucess = (Boolean) msg.obj;
			if (isInserSucess) {
				
//				ViewUtil.showToast(getActivity(), "sucess");
			}else{
				ViewUtil.showToast(getActivity(), "dealAnswerResultHandler failure");
			}
			
		}
	};
	
	private Handler updateHsdDataResearchStatusHandler = new Handler(){
		public void handleMessage(Message msg) {
			boolean isUpdate = (Boolean) msg.obj;
			if (isUpdate) {
				ViewUtil.showToast(getActivity(), "交卷成功");
				fragmentManager.popBackStack();
				
			}else{
				ViewUtil.showToast(getActivity(), "交卷失败");
			}
			
		}
	};
	
	private View.OnClickListener onMyClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.back:
				fragmentManager.popBackStack();
				break;
			case R.id.right:
				/**
				 * 交卷
				 */
			    DBController.getInstance().updateHsdDataResearchFinishStatus(getActivity(), surveyuuid, researchuuid, updateHsdDataResearchStatusHandler);
				break;
			case R.id.arrow_left_layout:
				
				if (currentSortIndex == 1) {
					ViewUtil.showToast(getActivity(), "当前已经是第一题");
				}else {
					currentSortIndex--;
					DBController.getInstance().getHsdModelQuestionObject(surveyuuid,String.valueOf(currentSortIndex),getActivity(), hsdModelQuestionHandler);
				}
				break;
			case R.id.arrow_right_layout:
				if (currentSortIndex == questionCount) {
					ViewUtil.showToast(getActivity(), "当前已经是最后一题");
				}else if (!questionAnswer.isHasResult()) {
					ViewUtil.showToast(getActivity(), "请选择答案");
				}else {
					currentSortIndex++;
					DBController.getInstance().getHsdModelQuestionObject(surveyuuid,String.valueOf(currentSortIndex),getActivity(), hsdModelQuestionHandler);
				}
				break;
			default:
				break;
			}
		}
	};
	


}
